package weeny.pre.face;

import java.awt.Component;
import java.util.EventObject;

import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;
import javax.swing.tree.TreeCellEditor;

import weeny.pre.io.parser.EnumParser;

/**
 * A combo box which helps you edit the values in a column having a EnumParser.
 * You should listen this combobox by adding action listener.
 * It also implements {@link TableCellEditor} and {@link TreeCellEditor}, so you
 * can directly use it in JTable and JTree.
 * 
 * @author weeny
 *
 */
public class EnumComboBox extends JComboBox implements TableCellEditor, 
                                                       TreeCellEditor {
	private EnumParser ep = null;
	private DefaultCellEditor editor = null;
	public EnumComboBox() {
		editor = new DefaultCellEditor(this);
	}
	public EnumComboBox(EnumParser parser){
		this();
		resetEnum(parser);
	}
	public void resetEnum(EnumParser parser){
		String[] keys = parser.getKeys().toArray(new String[0]);
		DefaultComboBoxModel dcm = new DefaultComboBoxModel(keys);
		this.setModel(dcm);
		this.ep = parser;
	}
	
	public void addCellEditorListener(CellEditorListener arg0) {
		editor.addCellEditorListener(arg0);
	}
	public void cancelCellEditing() {
		editor.cancelCellEditing();
	}

	public Object getCellEditorValue() {
		return editor.getCellEditorValue();
	}

	public boolean isCellEditable(EventObject arg0) {
		return editor.isCellEditable(arg0);
	}

	public void removeCellEditorListener(CellEditorListener arg0) {
		editor.removeCellEditorListener(arg0);
	}

	public boolean shouldSelectCell(EventObject arg0) {
		return editor.shouldSelectCell(arg0);
	}

	public boolean stopCellEditing() {
		return editor.stopCellEditing();
	}

	public Component getTreeCellEditorComponent(JTree t, Object v,
			boolean sel, boolean expanded, boolean leaf, int row) {
		if (!ep.isLocked()) {
			//if it is not locked, every time edit this cell, we need to
			//collect all options again, hope it is not slow
			this.removeAll();
			String[] keys = ep.getKeys().toArray(new String[0]);
			DefaultComboBoxModel dcm = new DefaultComboBoxModel(keys);
			this.setModel(dcm);
			this.updateUI();
		}
		return editor.getTreeCellEditorComponent(t, v, sel, expanded, leaf, row);
	}
	public Component getTableCellEditorComponent(JTable t, Object value,
			boolean sel, int row, int col) {
		if (!ep.isLocked()) {
			//if it is not locked, every time edit this cell, we need to
			//collect all options again, hope it is not slow
			this.removeAll();
			String[] keys = ep.getKeys().toArray(new String[0]);
			DefaultComboBoxModel dcm = new DefaultComboBoxModel(keys);
			this.setModel(dcm);
			this.updateUI();
		}
		return editor.getTableCellEditorComponent(t, value, sel, row, col);
	}
}
